<?php
/**
 * 角色管理模块
 * @author sundongdong
 * @date 2016-07-06
 */
class System_RoleController extends Controller_NeedLogin {
	/**
	 * 初始化
	 */
	public function init() {
		parent::init();
		$this->roleModel = new System_Model_Role();
	}
	/**
	 * 角色列表
	 */
	public function indexAction() {
		// 获取get参数
		$getData = $this->getRequest()->getParams();
		$params['search_type'] = !empty($getData['search_type'])?$getData['search_type'] : '';
		$params['keyword'] = !empty($getData['keyword']) ? trim($getData['keyword']) : '';
		$params['page'] = !empty($getData['page']) ? $getData['page'] : 1;
		$params['perPage'] = $this->page_size;
		$data = $this->roleModel->getRoleList($params);
		$page_link = $this->page($data['totalCounts'],$this->page_size);
		$this->smarty->assign('page_link', $page_link);
		$this->smarty->assign('params', $params);
		$this->smarty->assign('datas', $data['lists']);
		$this->smarty->display('sys_role.tpl');
	}
	/**
	 * 新增/修改 角色
	 */
	public function replaceroleAction() {
		$data['role_name'] = $this->getRequest()->getParam('name');
		$data['description'] = $this->getRequest()->getParam('description');
		$data['role_id'] =  $this->getRequest()->getParam('role_id_edit');
		$user = $this->_session->user_name;
		$status = $this->roleModel->replaceRole($data, $user);
		echo json_encode($status);
	}
	/**
	 * 获取角色信息
	 */
	public function getroleAction() {
		$role_id = $this->getRequest()->getParam('id');
		$role = $this->roleModel->getRoleById($role_id);
		echo json_encode($role);
	}
	/**
	 * 删除角色
	 */
	public function delroleAction() {
		$role_id = $this->getRequest()->getParam('id');
		$role_name = $this->getRequest()->getParam('role');
		$status = $this->roleModel->delRole($role_id);
		echo json_encode($status);
	}
	/**
	 * 编辑权限
	 */
	public function authAction() {
		$role_id = $this->getRequest()->getParam('id');
		$privilege = new Application_Model_Privilege();
		if ($role_id != '') {
			$roleInfo = $this->roleModel->getRoleById($role_id);
			$this->smarty->assign('editInfo', $roleInfo);
			$updateId = $role_id;
			$rolePrivileges = $this->roleModel->getRolePrivilege($role_id);
		}
		$menuPrivilege = array();
		foreach ($rolePrivileges as $rolePrivilege) {
			$menuPrivilege[$rolePrivilege['element_id']] = 1;
		}
		$menus = $privilege->getAllMenu();
		$menuLevel = array();
		foreach ($menus as $menuKey => $menuValue) {
			if($menuValue['fid'] == 0){
				$menuValue['children'] = array();
				$menuLevel[$menuKey] = $menuValue;
				foreach ($menus as $secondMenuKey => $secondMenuValue) {
					if($secondMenuValue['fid'] == $menuValue['menu_id']){
						$secondMenuValue['children'] = array();
						$menuLevel[$menuKey]['children'][$secondMenuKey] = $secondMenuValue;
						foreach ($menus as $thirdMenuKey => $thirdMenuValue) {
							if($thirdMenuValue['fid'] == $secondMenuValue['menu_id']){
								$thirdMenuValue['children'] = array();
								$menuLevel[$menuKey]['children'][$secondMenuKey]['children'][$thirdMenuKey] = $thirdMenuValue;
								foreach($menus as $fourthMenuKey => $fourthMenuValue)
								{
									if($fourthMenuValue['fid'] == $thirdMenuValue['menu_id']){
										$menuLevel[$menuKey]['children'][$secondMenuKey]['children'][$thirdMenuKey]['children'][$fourthMenuKey] = $fourthMenuValue;
									}
								}
							}
						}
					}
				}
			}
		}

		$this->smarty->assign('updateId', $updateId);
		$this->smarty->assign('menus', $menuLevel);
		$this->smarty->assign('menuPrivilege', $menuPrivilege);
		$this->smarty->display('sys_role_authorization.tpl');
	}
	/**
	 * 保存权限
	 */
	public function setprivilegeAction() {
		$privileges = $this->getRequest()->getParam('privileges');
		$updateId = $this->getRequest()->getParam('updateId');
		if(is_array($privileges) && count($privileges)>0) {
			$user = $this->_session->user_name;
			$updatePrivilege = $this->roleModel->updatePrivilege($updateId, $privileges, $user);
			if ($updatePrivilege === true) {
				echo json_encode(array('status'=>1, 'content'=>'更新成功'));
			} else{
				echo json_encode(array('status'=>0, 'content'=>'更新失败'));
			}
		} else {
			echo json_encode(array('status'=>0, 'content'=>'参数错误'));
		}
	}
}